<?php


namespace app\api\controller;

use app\api\service\FengniaoService;
use think\Config;
use think\Controller;


class Fengniao extends FengniaoService
{
    //创建门店
    public function chain_store($chain_store_code = null, $chain_store_name = null, $contact_phone = null, $address = null, $position_source = null, $longitude = null, $latitude = null, $service_code = null)
    {
        if (empty($chain_store_code) or empty($chain_store_name) or empty($contact_phone) or empty($address) or empty($position_source) or empty($longitude) or empty($latitude) or empty($service_code)) {
            $chain_store_code = input('chain_store_code');
            $chain_store_name = input('chain_store_name');
            $contact_phone = input('contact_phone');
            $address = input('address');
            $position_source = input('position_source');
            $longitude = input('longitude');
            $latitude = input('latitude');
            $service_code = input('service_code');
        }
        $dataArray = [
            'chain_store_code' => $chain_store_code,    //门店编号(支持数字、字母的组合)
            'chain_store_name' => $chain_store_name,    //门店名称(支持汉字、符号、字母的组合)
            'contact_phone' => $contact_phone,          //门店联系信息(手机号或座机或400)
            'address' => $address,                      //门店地址(支持汉字、符号、字母的组合)
            'position_source' => $position_source,      //坐标属性（0:未知, 1:腾讯地图, 2:百度地图, 3:高德地图）
            'longitude' => $longitude,                  //门店经度(数字格式, 包括小数点, 取值范围0～180)
            'latitude' => $latitude,                    //门店纬度(数字格式, 包括小数点, 取值范围0～90)
            'service_code' => $service_code             //配送服务(1:蜂鸟配送, 2:蜂鸟优送)
        ];
        $dataJson = json_encode($dataArray, JSON_UNESCAPED_UNICODE);
        $urlencodeData = urlencode($dataJson);
        $add_url = '/v2/chain_store';

        $F = new FengniaoService();
        $res = $F->makeRequest($urlencodeData, $add_url);
        //{"code":"200","msg":"success","data":null}
        //{"code":"500110","msg":"该门店已存在，请确认后重新传递","data":{}}
        return json_decode($res, true);
    }

    //查询门店
    public function query_store($chain_store_code = null)
    {
        if (empty($chain_store_code)) {
            $chain_store_code = input('chain_store_code');
        }
        $arr[0] = $chain_store_code;
        $dataArray = [
            'chain_store_code' => $arr      //门店编码集合，JSON格式字符串（支持数字,字母的组合）
        ];

        $dataJson = json_encode($dataArray, JSON_UNESCAPED_UNICODE);
        $urlencodeData = urlencode($dataJson);
        $add_url = '/v2/chain_store/query';

        $F = new FengniaoService();
        $res = $F->makeRequest($urlencodeData, $add_url);
        //{"code":"200","msg":"success","data":[{"chain_store_code":"A0fda01","chain_store_name":"门发达店一","city":"临高","address":"上海市","contact_phone":"18680481168","latitude":"19.9124300000","longitude":"109.6907730000","position_source":3,"service_code":1,"status":1}]}
        return json_decode($res, true);
    }

    //蜂鸟配送
    public function add_order()
    {
        $dataArray = [

            //商家店铺信息
            'transport_info' => [
                'transport_name' => '饿了么Bod上海普陀1站',             //门店名称
                'transport_address' => '上海市普陀区近铁城市广场5楼',    //取货点地址
                'transport_longitude' => 121.5156496362,       //取货点经度，取值范围0～180
                'transport_latitude' => 31.2331643501,         //取货点纬度，取值范围0～90
                'position_source' => 1,                     //取货点经纬度来源, 1:腾讯地图, 2:百度地图, 3:高德地图
                'transport_tel' => '13900000000',         //取货点联系方式, 只支持手机号,400开头电话以及座机号码
                'transport_remark' => '备注'              //取货点备注 (非必须)
            ],
            //收货人收货信息
            'receiver_info' => [
                'receiver_name' => 'jiabuchong',             //收货人姓名
                'receiver_primary_phone' => '13900000000',    //收货人联系方式，只支持手机号，400开头电话，座机号码以及95013开头、长度13位的虚拟电话
                'receiver_second_phone' => '13911111111',     //收货人备用联系方式，只支持手机号，400开头电话，座机号码以及95013开头、长度13位的虚拟电话 (非)
                'receiver_address' => '太阳',                 //收货人地址
                'receiver_longitude' => 121.5156496362,       //收货人经度，取值范围0～180
                'receiver_latitude' => 31.2331643501,      //收货人纬度，取值范围0～90
                'position_source' => 3                   //收货人经纬度来源, 1:腾讯地图, 2:百度地图, 3:高德地图
            ],
            //货物信息
            'items_json' => [
                array(
                    'item_name' => '苹果',          //商品名称
                    'item_quantity' => 5,           //商品数量
                    'item_price' => 9.50,           //商品原价
                    'item_actual_price' => 10.00,     //商品实际支付金额
                    'is_need_package' => 1,           //是否需要ele打包 0:否 1:是
                    'is_agent_purchase' => 1          //是否代购 0:否
                ),
                array(
                    'item_name' => '香蕉',
                    'item_quantity' => 20,
                    'item_price' => 100.00,
                    'item_actual_price' => 300.59,
                    'is_need_package' => 1,
                    'is_agent_purchase' => 1
                )
            ],

            // 订单信息
            'partner_remark' => '天下萨拉',     //商户备注信息(非必须)
            'partner_order_code' => '1234567890xx124',    // 第三方订单号, 需唯一
            'notify_url' => 'http://vpcb-anubis-web-base-2.vm.elenet.me:5000',     //第三方回调 url地址
            'order_type' => 2,                //订单类型 1: 蜂鸟配送，支持90分钟内送达
            'order_total_amount' => 50.00,        //订单总金额（不包含商家的任何活动以及折扣的金额）
            'order_actual_amount' => 48.00,        //客户需要支付的金额
            'order_weight' => 12.0,       //订单总重量（kg），营业类型选定为果蔬生鲜、商店超市、其他三类时必填，大于0kg并且小于等于6kg (非必须)
            'is_invoiced' => 1,          //是否需要发票, 0:不需要, 1:需要
            'invoice' => '饿了么',        //发票抬头, 如果需要发票, 此项必填(非必须)
            'order_payment_status' => 1,    //订单支付状态 0:未支付 1:已支付
            'order_payment_method' => 1,    //订单支付方式 1:在线支付
            'require_payment_pay' => 50.00,   //需要代收时客户应付金额, 如果需要ele代收 此项必填(非必须)
            'goods_count' => 4,             //订单货物件数
            'is_agent_payment' => 1,        //是否需要ele代收 0:否
            'require_receive_time' => strtotime('+1 day') * 1000  //需要送达时间（毫秒).  注意这是毫秒数
        ];
        $dataJson = json_encode($dataArray, JSON_UNESCAPED_UNICODE);
        $urlencodeData = urlencode($dataJson);
        $add_url = '/v2/order';

        $F = new FengniaoService();
        $res = $F->makeRequest($urlencodeData, $add_url);
        return json_decode($res, true);
    }

    //测试
    public function test()
    {
        $res = $this->query_store('A0fda01');
        dump($res);
    }
}
